package com.ckjava.test.web;

import com.alibaba.fastjson.JSONObject;
import com.ckjava.test.aop.annotation.CheckPermission;
import com.ckjava.test.entity.WebApiLog;
import com.ckjava.test.service.WebApiLogService;
import com.ckjava.xutils.StringUtils;
import com.ckjava.xutils.http.HttpResponse;
import com.ckjava.xutils.http.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api
@RestController
@RequestMapping(value="/api/webApiLog", produces = "application/json;charset=utf-8")
public class WebApiLogController {
    
    @Autowired
    private WebApiLogService webApiLogService;

    @CheckPermission
    @ApiOperation("分页获取的数据")
    @GetMapping
    public HttpResponse<Page<WebApiLog>> getPage(
            @ApiParam("{\"sort\":{\"predicate\":\"createDate\",\"reverse\":true},\"search\":{\"predicateObject\":{\"alias\":\"\"}},\"pagination\":{\"start\":0,\"number\":10}}")
            @RequestParam(value = "params", required = false) String params) { // {"sort":{"predicate":"urls","reverse":true},"search":{"predicateObject":{"activity":"false"}},"pagination":{"start":10,"totalItemCount":0,"number":10,"numberOfPages":2}}) {

        WebApiLog webApiLog = new WebApiLog();

        if (StringUtils.isNotBlank(params)) {
            JSONObject paramsObj = JSONObject.parseObject(params);
            JSONObject sortObj = paramsObj.getJSONObject("sort"); // 排序字段
            if (!sortObj.isEmpty()) {
            }
            JSONObject searchObj = paramsObj.getJSONObject("search"); // 查询字段
            if (!searchObj.isEmpty()) {
                JSONObject predicateObject = searchObj.getJSONObject("predicateObject");
                if (!predicateObject.isEmpty()) {
                    if (StringUtils.isNotBlank(predicateObject.getString("opUser"))) { // 操作人
                        webApiLog.setOpUser(predicateObject.getString("opUser"));
                    }
                }
            }
            JSONObject pageObj = paramsObj.getJSONObject("pagination"); // 查询字段
            if (!pageObj.isEmpty()) {
                webApiLog.setStart(pageObj.getInteger("start"));
                webApiLog.setPageSize(pageObj.getInteger("number"));
            } else {
                webApiLog.setStart(0);
                webApiLog.setPageSize(30);
            }
        }

        List<WebApiLog> dataList = webApiLogService.getPage(webApiLog);
        Integer totalCount = webApiLogService.getCount(webApiLog);
        return HttpResponse.getReturn(Page.getPage(webApiLog.getStart(), webApiLog.getPageSize(), totalCount, dataList), HttpResponse.SUCCESS, null);
    }


    @ApiOperation("根据主键id获取单个数据")
    @GetMapping(value = "/{id}")
    public HttpResponse<WebApiLog> get(
            @PathVariable Long id) {
        WebApiLog webApiLog = webApiLogService.get(id);
        return HttpResponse.getReturn(webApiLog, HttpResponse.SUCCESS, null);
    }

    @CheckPermission
    @ApiOperation("新增或者修改单个数据")
    @PostMapping
    public HttpResponse<Long> save(
            @RequestBody WebApiLog webApiLog) throws Exception {
        Long id = webApiLogService.saveOrUpdate(webApiLog);
        if (id == null) {
            return HttpResponse.getReturn(null, HttpResponse.FAIL, null);
        } else {
            return HttpResponse.getReturn(id, HttpResponse.SUCCESS, null);
        }
    }

    @CheckPermission
    @ApiOperation("删除单个数据")
    @DeleteMapping(value = "/{id}")
    public HttpResponse<Integer> delete(
            @PathVariable Long id) {
        Integer result = webApiLogService.delete(id);
        if (result > 0) {
            return HttpResponse.getReturn(result, HttpResponse.SUCCESS, null);
        } else {
            return HttpResponse.getReturn(result, HttpResponse.FAIL, "删除失败");
        }
    }

}
